package com.hrsoft.modules.sys.controller;

import java.util.*;

import com.hrsoft.common.utils.ShiroUtils;
import com.hrsoft.modules.sys.dto.DepartmentTreeDto;
import com.hrsoft.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hrsoft.modules.sys.entity.SysDepartmentEntity;
import com.hrsoft.modules.sys.service.SysDepartmentService;
import com.hrsoft.common.utils.PageUtils;
import com.hrsoft.common.utils.R;



/**
 * 部门管理
 *
 * @author garfield.yue
 * @email yuedefeng101@163.com
 * @date 2020-04-23 22:41:38
 */
@RestController
@RequestMapping("sys/sysdepartment")
public class SysDepartmentController {
    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysdepartment:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysDepartmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:sysdepartment:info")
    public R info(@PathVariable("id") Long id){
		SysDepartmentEntity sysDepartment = sysDepartmentService.getById(id);

        return R.ok().put("sysDepartment", sysDepartment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysdepartment:save")
    public R save(@RequestBody SysDepartmentEntity sysDepartment){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        sysDepartment.setCreateUserId( Integer.parseInt(userEntity.getUserId().toString()) );
        sysDepartment.setCreatedBy(userEntity.getUsername());
        sysDepartment.setCreatedOn(new Date());
		sysDepartmentService.save(sysDepartment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysdepartment:update")
    public R update(@RequestBody SysDepartmentEntity sysDepartment){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        sysDepartment.setModifiedUserId( Integer.parseInt(userEntity.getUserId().toString()) );
        sysDepartment.setModifiedBy(userEntity.getUsername());
        sysDepartment.setModifiedOn(new Date());

		sysDepartmentService.updateById(sysDepartment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysdepartment:delete")
    public R delete(@RequestBody Long[] ids){
		sysDepartmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/tree-list/{parentId}")
    public R treeList(@PathVariable("parentId") Integer parentId){
        DepartmentTreeDto departTreeDto = new DepartmentTreeDto();
        departTreeDto.setLabel("根节点");
        departTreeDto.setId(0);
        List<DepartmentTreeDto> goodsTreeDtos = sysDepartmentService.getTreeList(parentId);
        departTreeDto.setChildren(goodsTreeDtos);
        List<DepartmentTreeDto> rootList = new ArrayList<>();
        rootList.add(departTreeDto);
        return R.ok().put("departTree", rootList);
    }

}
